package com.yao.sys.service.impl;

import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yao.common.constant.SysConst;
import com.yao.common.exception.BusinessException;
import com.yao.common.util.ThrowUtils;
import com.yao.sys.entity.dto.RoleDto;
import com.yao.sys.entity.dto.RoleMenuRelDto;
import com.yao.sys.mapper.RoleMapper;
import com.yao.sys.mapper.sql.RoleWrapper;
import com.yao.sys.service.MenuService;
import com.yao.sys.service.RoleMenuRelService;
import com.yao.sys.service.RoleService;

/**
 * 角色表 接口实现
 *
 * @author autoCode
 * @version 1.0.0-SNAPSHOT
 * @date 2020-04-09
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleDto> implements RoleService {

    @Autowired
    private RoleMenuRelService roleMenuRelService;

    @Autowired
    private MenuService menuService;


    @Override
    public void add(RoleDto dto) {
        Wrapper<RoleDto> query = RoleWrapper.byNameOrKeys(dto.getName(), dto.getKeys(), null);
        RoleDto role = this.getOne(query);
        if (Objects.nonNull(role)) {
            throw new BusinessException("已存在相同名称[{}]或标示[{}]的角色", dto.getName(), dto.getKeys());
        }

        if (!this.save(dto)) {
            throw new BusinessException("新增角色失败.");
        }

        // 角色-菜单关系
        if (!roleMenuRelService.saveBatch(RoleDto.ofMenuIds(dto, SysConst.DEFALUT_OPERATOR))) {
            throw new BusinessException("新增角色-菜单关系失败.");
        }


    }

    @Override
    public void modifyById(RoleDto dto) {
        Wrapper<RoleDto> query = RoleWrapper.byNameOrKeys(dto.getName(), dto.getKeys(), dto.getId());
        RoleDto role = this.getOne(query);
        if (Objects.nonNull(role)) {
            throw new BusinessException("已存在相同名称[{}]或标示[{}]的角色", dto.getName(), dto.getKeys());
        }

        if (!this.updateById(dto)) {
            throw new BusinessException("更新角色失败.");
        }

        // 角色-菜单关系
        LambdaQueryWrapper<RoleMenuRelDto> deleteWrapper = Wrappers.lambdaQuery();
        deleteWrapper.eq(RoleMenuRelDto::getRoleId, dto.getId());
        roleMenuRelService.remove(deleteWrapper);

        if (!roleMenuRelService.saveBatch(RoleDto.ofMenuIds(dto, SysConst.DEFALUT_OPERATOR))) {
            throw new BusinessException("新增角色-菜单关系失败.");
        }

    }

    @Override
    public RoleDto byId(Integer id) {

        RoleDto role = ThrowUtils.isNull(this.getById(id), "id:[{}]查询不到角色.", id);

        LambdaQueryWrapper<RoleMenuRelDto> query = Wrappers.lambdaQuery();
        query.eq(RoleMenuRelDto::getRoleId, role.getId());
        // 已选菜单数
        role.setMenuIds(roleMenuRelService.list(query).stream().map(RoleMenuRelDto::getMenuId).map(Objects::toString).collect(Collectors.joining(",")));


        return role;
    }

}